#
# @lc app=leetcode.cn id=404 lang=python3
#
# [404] 左叶子之和
#
from typing import Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
# @lc code=start
# Definition for a binary tree node.
class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
        self.sum = 0
        self.preorder(root)
        return self.sum
    
    def preorder(self, root: TreeNode, is_left: bool=False) -> None:
        if root is None:
            return 
        if is_left and root.left is None and root.right is None:
            self.sum += root.val
        self.preorder(root.left, is_left=True)
        self.preorder(root.right, is_left=False)
# @lc code=end

